package com.liruo.learn.spring.mvc.config;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

/**
 * @Author:liruo
 * @Date:2023-05-09-12:49:48
 * @Desc
 */
@Configuration
public class ValidConfig {



    /**
     * 自己配置Validator
     */
    public class ParamValidatorConfig {

//        @Bean
        public Validator validator() {
            ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                    .configure()
                    //failFast：只要出现校验失败的情况，就立即结束校验，不再进行后续的校验。
                    .failFast(true)
                    .buildValidatorFactory();
            return validatorFactory.getValidator();
        }

//        @Bean
        public MethodValidationPostProcessor methodValidationPostProcessor() {
            MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
            methodValidationPostProcessor.setValidator(validator());
            return methodValidationPostProcessor;
        }

    }
}
